package com.qq2008.game.stzb.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/***
 * Cookie工具
 */
public class CookieUtil {

    // 默认Cookie保存时长, 7天
    private static final int DEFAULT_COOKIE_TIME = 7 * 24 * 60 * 60;

    /***
     * 添加Cookie
     * @param response Response
     * @param cookieName Key
     * @param cookieValue Value
     */
    public static void setCookie(HttpServletResponse response,
                                 String cookieName,
                                 String cookieValue) {
        try {
            Cookie cookie = new Cookie(cookieName, URLEncoder.encode(cookieValue, "utf-8"));
            cookie.setPath("/");
            cookie.setMaxAge(DEFAULT_COOKIE_TIME);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /***
     * 添加Cookie
     * @param response Response
     * @param cookieName Key
     * @param cookieValue Value
     * @param cookieMaxAge 有效时长
     */
    public static void setCookie(HttpServletResponse response,
                                 String cookieName,
                                 String cookieValue,
                                 int cookieMaxAge) {
        try {
            Cookie cookie = new Cookie(cookieName, URLEncoder.encode(cookieValue, "utf-8"));
            cookie.setPath("/");
            cookie.setMaxAge(cookieMaxAge);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /***
     * 添加Cookie
     * @param response Response
     * @param cookieName Key
     * @param cookieValue Value
     * @param cookieMaxAge 有效时长
     * @param domain 域名
     */
    public static void setCookie(HttpServletResponse response,
                                 String cookieName,
                                 String cookieValue,
                                 int cookieMaxAge,
                                 String domain) {
        try {
            Cookie cookie = new Cookie(cookieName, URLEncoder.encode(cookieValue, "utf-8"));
            cookie.setDomain(domain);
            cookie.setPath("/");
            cookie.setMaxAge(cookieMaxAge);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /***
     * 根据cookieName获取指定cookie value
     * @param request Request
     * @param cookieName CookieName
     * @return CookieValue | ""
     */
    public static String getValue(HttpServletRequest request, String cookieName) {
        Cookie cookie = getCookie(request, cookieName);
        if (null != cookie) {
            try {
                return URLDecoder.decode(cookie.getValue(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /***
     * 根据cookieName获取指定cookie
     * @param request Request
     * @param cookieName CookieName
     * @return Cookie | null
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie;
                }
            }
        }
        return null;
    }

    /**
     * 清除指定name的cookie
     */
    public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        if (null != getValue(request, cookieName)) {
            Cookie cookie = new Cookie(cookieName, null);
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }

    /**
     * 清除所有cookie
     *
     * @param request  Request
     * @param response Response
     */
    public static void clearAll(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            cookie.setValue(null);
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }
}
